package com.browsesoft.util;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.oa.DisposeSelectResult;

public class ComponentHelper {
	/**
	 * 根据ID号得到对应的选择器
	 */
	public static DisposeSelectResult getDisposeSelectResult(
			HttpServletRequest request) {
		HTMLBasicComponent fromcomponent = getComponent(request);
		// 判断此组件是否是一个结果处理接口
		if (fromcomponent instanceof DisposeSelectResult) {
			return (DisposeSelectResult) fromcomponent;
		} else {
			return (DisposeSelectResult) fromcomponent.getModel();
		}
	}

	public static HTMLBasicComponent getComponent(HttpServletRequest request) {
		// 得到传递过来的上一个组件的id号
		String compid = request.getParameter("compid");
		if (compid == null || compid.equals("")) {
			return null;
		}
		// 根据id号得到组件
		HTMLBasicComponent fromcomponent = HTMLBasicComponent.getComponentByID(
				request, compid);
		return fromcomponent;
	}

	/**
	 * 
	 * @param element
	 * @return
	 */
	public static Entity getEntity(Element element, AdvancedSimpleEntity model) {
		// 取属性名
		String attrName = element.getAttribute("sourcename");
		// 取实体
		Entity entity = model.getEntity(attrName);
		return entity;
	}
}
